#include "sec_common.h"
#include "hard.h"
#define FUNCTION_TEST   1
#define PERFORMANCE_TEST 0






#define AES_TEST    0
#define SM4_TEST    0
#define DES_TEST    0
#define THREE_DES_TEST   0
#define SSF33_TEST  0
#define SM6_TEST    0
#define SM1_TEST    0
#define HMAC_TEST   1
#define HASH_TEST   0
inline void noprintf(const char *fmt,...)
{
}

 void fprintf_error(const char *fmt,...)
{
	struct timeval tv;
	va_list args;
	if(erroutfd==NULL)
		return;
	va_start(args, fmt);
	gettimeofday(&tv, NULL);
	fprintf(erroutfd, "pid %x,time %d,FILE %s,LINE %d,",(unsigned int)getpid(),(int)tv.tv_sec,__FILE__,__LINE__);
	fprintf(erroutfd,fmt,args);
	va_end(args);
}

void errout_close()
{
	if(erroutfd==NULL)
		return;
	fclose(erroutfd);
}

ssl_main()
{

}

void wait_another(void)
{
	sleep(1);
}


hard_soft_main()
{
	struct timeval now;
	unsigned int seed;
	int fd=-1;
	int tlog_cnt=0;

	int i=0,forever=0;

	gettimeofday(&now, NULL);
	seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
	srand(seed);

	debug("hard_soft test start:\n");
	fd = open("/dev/csec",O_RDWR);
	if(fd<0)
	{
		debug("open dev csec error\n");
		return 0;
	}

	i = _IOC_SIZEBITS;
	debug("IOC_SIZEBITS is %x\n",i);

	if(TCNT == 0)
		forever=1;
	else
		i=TCNT;
	do
	{
		i--;
		if(tlog_cnt%TLOG_CNT==0)
		{
			tlog("\npid %x,t %x\n",getpid(),tlog_cnt);
		}
		tlog_cnt++;
#if PERFORMANCE_TEST
	hash_sm3_ssl_hard_speed(fd);
	hash_sha1_ssl_hard_speed(fd);
	hash_sha256_ssl_hard_speed(fd);
	sm6_cbc_ssl_hard_key32_speed(fd);
	ssf33_cbc_ssl_hard_key32_speed(fd);
	des_cbc_ssl_hard_key32_speed(fd);

	sm4_cbc_ssl_hard_key32_speed(fd);
	aes_cbc_ssl_hard_key32_speed(fd);
	sm1_cbc_ssl_hard_key32_speed(fd);
	hmac_sha1_ssl_hard_speed(fd);
	hmac_sha256_ssl_hard_speed(fd);
	hmac_sm3_ssl_hard_speed(fd);

	three_des_cbc_ssl_hard_key32_speed(fd);

#endif
#if FUNCTION_TEST
#if AES_TEST
		aes_ecb_ssl_hard_test( fd,16);
		aes_cfb_ssl_hard_test( fd,16);
		aes_ofb_ssl_hard_test( fd,16);
		aes_cbc_ssl_hard_test( fd,16);
		aes_ctr_ssl_hard_test( fd,16);
		//aes_cbc_ssl_hard_test( fd,24);
		//aes_cbc_ssl_hard_test( fd,32);
#endif
#if SM4_TEST
		sm4_ecb_ssl_hard_test( fd);
		sm4_cfb_ssl_hard_test( fd);
		sm4_ofb_ssl_hard_test( fd);
		sm4_cbc_ssl_hard_test( fd);
		sm4_ctr_ssl_hard_test( fd);
#endif
#if DES_TEST
		des_ecb_ssl_hard_test( fd);
		des_cbc_ssl_hard_test( fd);
		des_ofb_ssl_hard_test( fd);
		des_cfb_ssl_hard_test( fd);
		des_ctr_ssl_hard_test( fd);
#endif
#if THREE_DES_TEST
		three_des_ecb_ssl_hard_test( fd);
		three_des_cbc_ssl_hard_test( fd);
		three_des_ofb_ssl_hard_test( fd);
		three_des_cfb_ssl_hard_test( fd);
		three_des_ctr_ssl_hard_test( fd);
#endif

#if SSF33_TEST
		ssf33_ecb_ssl_hard_test( fd);
		ssf33_cbc_ssl_hard_test( fd);
		ssf33_cfb_ssl_hard_test( fd);
		ssf33_ofb_ssl_hard_test( fd);
		ssf33_ctr_ssl_hard_test( fd);
#endif
#if SM6_TEST
		sm6_ecb_ssl_hard_test( fd);
		sm6_cbc_ssl_hard_test( fd);
		sm6_cfb_ssl_hard_test( fd);
		sm6_ofb_ssl_hard_test( fd);
		sm6_ctr_ssl_hard_test( fd);
#endif
#if SM1_TEST
		sm1_ecb_ssl_hard_test( fd);
		sm1_cbc_ssl_hard_test( fd);
		sm1_cfb_ssl_hard_test( fd);
		sm1_ofb_ssl_hard_test( fd);
		sm1_ctr_ssl_hard_test( fd);
#endif
#if HMAC_TEST
		//hmac_md5_ssl_hard_test( fd);
		//hmac_sha0_ssl_hard_test( fd);
		//hmac_sha1_ssl_hard_test( fd);
		//hmac_sha224_ssl_hard_test( fd);
		//hmac_sha256_ssl_hard_test( fd);
		//hmac_sha384_ssl_hard_test( fd);
		//hmac_sha512_ssl_hard_test( fd);
		hmac_sm3_ssl_hard_test( fd);
#endif
#if HASH_TEST
		hash_sha1_ssl_hard_test( fd);
		hash_sha256_ssl_hard_test( fd);
		hash_sha384_ssl_hard_test( fd);
		hash_sha512_ssl_hard_test( fd);
		hash_sm3_ssl_hard_test( fd);
#endif
#endif
	}while(forever|i);
	debug("\nhard_soft test over!\n");
	close(fd);
	return 0;

}

struct usem_st *usem;
struct usem_st *dsem;

void all_stop(int signo)
{
	errout_close(erroutfd);
	sem_close(usem->sem);
	sem_close(dsem->sem);
	free(usem);
	free(dsem);
	exit(0);
}

main()
{

	usem = malloc(sizeof(struct usem_st));
	dsem = malloc(sizeof(struct usem_st));

	snprintf(usem->sname,sizeof(usem->sname),"u%ld",(long)getpid());
	snprintf(dsem->sname,sizeof(dsem->sname),"d%ld",(long)getpid());

	usem->sem = sem_open(usem->sname,O_CREAT|O_EXCL,S_IRWXU,1);
	if(usem->sem == SEM_FAILED)
	{
		debug("semphore creat failed!\n");
		return -1;
	}

	dsem->sem = sem_open(dsem->sname,O_CREAT|O_EXCL,S_IRWXU,MAX_SEM_CNT);
	if(dsem->sem == SEM_FAILED)
	{
		debug("semphore creat failed2!\n");
		return -1;
	}

	signal(SIGINT,all_stop);
	erroutfd = fopen("./log.out","a+");
	errout(" APP TEST START\n");

	//ssl_main();
	hard_soft_main();

	sem_close(usem->sem);
	sem_close(dsem->sem);
	free(usem);
	free(dsem);
	exit(0);
}
